package com.stark.friendrequest;

import java.util.Calendar;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class RequestAvailable extends Activity {

	private ImageButton requestButton;
	private SmsManager smsManager;
	private SharedPreferences prefs;
	private String prefName, prefNum;
	private String timeString, smsText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.request_available);
		
		//Create the SMSManager with default settings.
        smsManager = SmsManager.getDefault();
		
		requestButton=(ImageButton)findViewById(R.id.request_available_button);
		requestButton.setOnClickListener(new Button.OnClickListener(){
		public void onClick(View arg0) {
			//Provide feedback when the button is pressed.
			requestButton.setBackgroundResource(R.drawable.buttonpressed);
			
			prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
			
			prefName = prefs.getString("friend_name","Enter Name");
	    	prefNum = prefs.getString("friend_number","Enter Number");
			
			smsText = "Tier One Friend Request has been Initiated, Contact Friend Immediately";
			
			System.out.println("****************"+prefNum+"***   "+prefName);
			
			timeString = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
			
			SharedPreferences.Editor editor = prefs.edit();
            editor.putString("time_remaining", timeString);
            editor.commit();
			
			//Send the text using the smsManager.
		    smsManager.sendTextMessage(prefNum.toString(), null, smsText, null, null);
		     
		    //Show a toast notification to inform the user a test has been sent
		    Context context = getApplicationContext();
		    CharSequence text = "Tier One Friend Request has been sent to " + prefName;
		    int duration = Toast.LENGTH_LONG;
		    Toast toast = Toast.makeText(context, text, duration);
		    toast.show();
		    
		    Intent i = new Intent(RequestAvailable.this, MainActivity.class);
			startActivity(i);
	    }}); 
	}
	
/*	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}*/

}